package com.tacuati.ui;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EventObject;

import javax.swing.AbstractCellEditor;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.tree.TreeCellEditor;

public class ContentTreeCellEditor extends AbstractCellEditor implements TreeCellEditor, ActionListener {

	private static final long serialVersionUID = 1L;

	JTextField textField;  
    
	public ContentTreeCellEditor() {
		textField = new JTextField();
	    textField.addActionListener(this); 
	}

	@Override
	public boolean isCellEditable(EventObject event) {
		ContentTreeNode node = (ContentTreeNode) ((JTree)event.getSource()).getLastSelectedPathComponent();
	    return node.isEditable();
	}
	
	@Override
	public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row) {
		textField.setText(((ContentTreeNode) value).getNodeName());
        return textField;  	
    }

	@Override
	public Object getCellEditorValue() {
		return textField.getText();  
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		super.stopCellEditing(); 
	}
	
}
